package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.ExportInfo;
import com.itheima.service.MemberService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

//导出会员信息
@RequestMapping("/report")
@RestController
public class ExportMemberInfoController {
    @Reference
    private MemberService memberService;

    @RequestMapping("/MemberInfoDetails")
    public Result exportMemberInfo(HttpServletRequest request, HttpServletResponse response, String ids) throws Exception {


        //获取前台传入的数据，通过“，”拆分装入集合
        String[] arr = ids.split(",");
        //将会员ID分装到集合中
        List<Integer> list = new ArrayList<>();
        for (String s : arr) {
            int i = Integer.parseInt(s);
            list.add(i);
        }
      //  List<ExportInfo> exportInfoList = new ArrayList<>();
        try {
            List<ExportInfo> exportInfo = memberService.getMemberInfoData(list);



            //取出返回结果数据，准备将报表数据写入到Excel文件中

            //读取模板文件创建Excel表格对象
            //获取excel模板文件的绝对路径(File.separator文件分割符，不同的操作系统分隔符不同)
            String filePath = request.getSession().getServletContext().getRealPath("template") + File.separator + "MemberInfoDetails.xlsx";
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(filePath)));

            //获取第一个工作表
            XSSFSheet sheet = workbook.getSheetAt(0);

            int rowNum = 1;


            for (int i = 0; i < exportInfo.size(); i++) {
                XSSFRow row = sheet.getRow(rowNum);


           /* for (int i1 = 0; i1 < list.size(); i1++) {
                XSSFRow row = sheet.getRow(rowNum);*/

              /*  for (ExportInfo info : memberInfoData) {
                   name = info.getMemberName();
                }*/

                /*row.getSheet().createRow(i + 1);*/
                //ExportInfo exportInfo = memberInfoData.get(i1);

                String name = "";
                if (exportInfo.get(i).getMemberName() == null) {
                    name = "";
                } else {
                    name = exportInfo.get(i).getMemberName();
                }

                String sex = "";
                if (exportInfo.get(i).getSex() == null) {
                    sex = "";
                } else {
                    sex = exportInfo.get(i).getSex();
                    int i1 = Integer.parseInt(sex);
                    if (i1==0){
                        sex="男";
                    }else{
                        sex="女";
                    }
                }


                String phoneNumber = "";
                if (exportInfo.get(i).getPhoneNumber() == null) {
                    phoneNumber = "";
                } else {
                    phoneNumber = exportInfo.get(i).getPhoneNumber();
                }

                String address = "";
                if (exportInfo.get(i).getAddress() == null) {
                    address = "";
                } else {
                    address = exportInfo.get(i).getAddress();
                }

                String setmeal = "";
                if (exportInfo.get(i).getMenu() == null) {
                    setmeal = "";
                } else {
                    setmeal = exportInfo.get(i).getMenu();
                }
                String checkgroup ="";
                if (exportInfo.get(i).getCheckGroup()== null) {
                    checkgroup="";
                } else {
                    checkgroup = exportInfo.get(i).getMenu();
                }
                String checkitem ="";
                if (exportInfo.get(i).getCheckGroup()== null) {
                    checkitem="";
                } else {
                    checkitem = exportInfo.get(i).getMenu();
                }


               //创建单元格
                    row.getCell(0).setCellValue(name);
                    row.getCell(1).setCellValue(sex);
                    row.getCell(2).setCellValue(phoneNumber);
                    row.getCell(3).setCellValue(address);
                    row.getCell(4).setCellValue(setmeal);
                    row.getCell(5).setCellValue(checkgroup);
                    row.getCell(6).setCellValue(checkitem);
                    rowNum++;

            /*    row.getCell(0).setCellValue(name);
                row.getCell(1).setCellValue("1");
                row.getCell(2).setCellValue("1");
                row.getCell(3).setCellValue("1");
                row.getCell(4).setCellValue("1");

                row.getCell(5).setCellValue("1");
                row.getCell(6).setCellValue("1");
                rowNum++;
*/
            }

            //通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            //设置响应头一直盯excel类型写出
            response.setContentType("application/vnd.ms-excel");
            //设置响应头以附件形式下载
            response.setHeader("content-Disposition", "attachment;filename=MemberInfoDetails.xlsx");
            //将文件写出
            workbook.write(out);

            //刷新并关闭流
            out.flush();
            out.close();
            workbook.close();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS);
        } catch (Exception exception) {
            exception.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
}



